/**
 * <p>Title: MaterialPackagePropertyDaoImpl</p>
 * <p>Description: The Implement of MaterialPackagePropertyDao</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author qianpengji
 * @version 1.0
 * <pre>Histroy:
 *       2016-11-23  qianpengji  Create
 *</pre>
*/
package cn.jhg.account.persist;

import javax.annotation.Resource;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import cn.jhg.account.entity.MaterialPackage;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;


@Repository 
public class MaterialPackagePropertyDaoImpl implements MaterialPackagePropertyDao{
	
	Logger logger=LoggerFactory.getLogger(MaterialPackagePropertyDaoImpl.class);
	@Resource
	private MongoTemplate mongoTemplate;

	/**
	 * 增加物料属性
	 * @param obId
	 * @param meta
	 * @return erroCode
	 */
	@Override
	public int addPackage(ObjectId obId,Document meta) {
		try{
			DBObject object = new BasicDBObject();//设置需要插入到数据库的文档对象
			if(obId==null){
				logger.error("[ERR223197781] obId不存在,添加物料件属性失败");
				return 223197781;//添加元数据失败
			}
			object.put("_id",obId);
			object.put("metadata", meta);
			mongoTemplate.insert(object,"material_package_property");
			logger.info("[INF223199465] 添加物料件属性成功");
			return 0;//元数据添加成功
		}catch(Exception e){
			logger.error("[ERR223105883] 添加物料件属性失败",e.getMessage());
			return 223105883;//异常错误代码
		}
	}

	/**
	 * 更新物料属性
	 * @param obId
	 * @param meta
	 * @return erroCode
	 */
	@Override
	public int updatePackage(ObjectId obId, Document meta) {
		try{
			Update update=new Update();
			update.set("metadata", meta);
			mongoTemplate.updateMulti(new Query(Criteria.where("_id").is(obId)),update, "material_package_property");
			logger.info("[INF223199924] 物料件属性修改成功");
			return 0;//物料件属性修改成功
		}catch(Exception e){
			logger.error("[ERR223105899] 物料件属性修改失败",e.getMessage());
			return 223105899;//异常错误代码
		}
	}
	

	/**
	 * 删除物料属性
	 * @param obId
	 * @return erroCode
	 */
	@Override
	public int deletePackage(ObjectId obId) {
		try {
			mongoTemplate.remove(new Query(Criteria.where("_id").is(obId)), MaterialPackage.class, "material_package_property");
			logger.info("[INF223199952] 物料件属性删除成功");
			return 0;//物料件属性删除成功
		} catch (Exception e) {
			logger.error("[ERR223119590] 物料件属性删除失败",e.getMessage());
			return 223119590;//异常错误代码
		}

	}

	/**
	 * 查询物料属性
	 * @param obId
	 * @return MaterualPackageProrerty mongoDB中物料件实体
	 */
	@Override
	public Document queryPackage(ObjectId obId) {
		Document doc=new Document();
		Document qp=new Document();
		try{	
			qp = mongoTemplate.findById(obId, Document.class, "material_package_property");	
			if(qp==null){
				doc.append("ErrorCode", 224405343);//obId不存在，查询元数据失败
				logger.error("[ERR224405343] obId不存在,查询元数据失败");
			}else{
				doc.append("metadata", qp);
				doc.append("ErrorCode", 0);
				logger.info("[INF223197682] 查询物料件属性成功");
			}		
			return doc;
		}catch(Exception e){
			logger.error("[ERR223197693] 查询物料件属性失败",e.getMessage());
			return null;
		}
	}

}
